package com.zdsoft.blp.zongbuju.shouye.bianmin.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.dialog.CustomDailog;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.zongbuju.shouye.bianmin.adapter.BMbmdhAdapter;
import com.zdsoft.blp.zongbuju.shouye.bianmin.entity.BianMin;
import com.zdsoft.blp.zongbuju.shouye.bianmin.adapter.BianminAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/30.
 */
public class BianminActivity extends BaseActivity implements NetCallback{
    private ListView lv_bm;
    private TextView tv_title;
    private List<BianMin> list;
    private BianMin bianMin;
    @Override
    protected void init() {
        //CustomDailog.showDiaLog(BianminActivity.this);
        setContentView(R.layout.bianmin);
        lv_bm = find(R.id.lv_bm);
        tv_title = find(R.id.tv_title);
        tv_title.setText("便民");

        Net net=new Net();
        net.url= API.BMINFO_URL;
        net.nameSpace=API.BMINFO_NAMESPACE;
        net.methodName=API.COLUMNCHILDLIST;
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        String communityCode = sp.getString("communityCode", "00001");
        Map<String,Object> map=new HashMap<>();
        map.put("communityCode",communityCode);
        map.put("columnCode","xx");
        net.parms=map;
        getServer(net,this);

        //ListView添加数据源
       // lv_bm.setAdapter(new BianminAdapter(this,getDate()));
        //Item点击事件，跳转至相对应的页面
        lv_bm.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            Intent  intent;
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                   String ititle=list.get(position).getTitle();
                    switch (ititle){
                        case "社区活动":
                            intent = new Intent(BianminActivity.this,BMsqhdActivity.class);
                            startActivity(intent);
                            break;
                        case "办事指南":
                            intent = new Intent(BianminActivity.this,BMbsznActivity.class);
                            startActivity(intent);
                            break;
                        case "便民电话":
                            intent = new Intent(BianminActivity.this,BMbmdhActivity.class);
                            intent.putExtra("id",list.get(position).getId());
                            startActivity(intent);
                            break;
                        case "出行查询":
                            intent = new Intent(BianminActivity.this,BMcxcxActivity.class);
                            intent.putExtra("code",list.get(position).getCode());
                            startActivity(intent);
                            break;
                        case "周边优惠":
                            intent = new Intent(BianminActivity.this,BMzbyhActivity.class);
                            intent.putExtra("code",list.get(position).getCode());
                            startActivity(intent);
                            break;
                    }
            }

        });

    }


    @Override
    protected void onResume() {
        super.onResume();
        //CustomDailog.disMissDiaLog();
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        Log.e("bianmin",res);
        list=jiexi(res);
        lv_bm.setAdapter(new BianminAdapter(this,list));
    }

    private List<BianMin> jiexi(String res) {
        list=new ArrayList<>();
        try {
            JSONObject object=new JSONObject(res);
            JSONArray array=object.getJSONArray("value");
            for(int i=0;i<array.length();i++){
                JSONObject object2=array.getJSONObject(i);
                String id=object2.getString("id");
                String code=object2.getString("code");
                String content=object2.getString("desc");
                String title=object2.getString("name");
                String picpath=object2.getString("bmColumnImgPath");
                bianMin=new BianMin(id,title,content,picpath,code);
                list.add(bianMin);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return list;
    }
}
